layui.define(function(exports) {
    function DateFilter() {}

    /**
     * @author: ares
     * @date: 2018/12/6 14:48
     * @description: 前置加0
     * @param {string}n
     * @returns {*}
     */
    function preZero(number) {
        var _number = number.toString();
        return _number.length == 1 ? ('0' + _number) : _number;
    }

    /**
     * @author: ares
     * @date: 2018/12/6 14:51
     * @description: 格式化日期
     * @param {Date | number | string}value
     * @returns {object}
     */
    function _dateFormat(value) {
        if(typeof value == 'string') value = value.replace(/-/g,'/');
        var date = new Date(value),
            year = date.getFullYear(),
            month = date.getMonth() + 1,
            day = date.getDate(),
            hour = date.getHours(),
            minute = date.getMinutes(),
            second = date.getSeconds();
        return {
            date: [year, month, day],
            time: [hour, minute, second]
        };
    }

    DateFilter.prototype = {
        constructor: DateFilter,

        /**
         * @author: ares
         * @date: 2018/12/6 15:10
         * @description: 格式化日期
         * @param {Date | number | string}value
         * @param {string}joinSymbol    连接符
         * @param {string}unFilterSymbol    适配失败时返回的字符串
         * @returns {string}
         */
        dateFormat: function(value, joinSymbol, unFilterSymbol) {
            var joinStr = joinSymbol || '-',
                unFilterSymbol = unFilterSymbol || '-';
            if (!value) return unFilterSymbol;
            var date = [],
                data = _dateFormat(value);
            for (var i = 0; i < data.date.length; i++) {
                date.push(preZero(data.date[i]));
            }
            return date.join(joinStr);
        },


        /**
         * @author: ares
         * @date: 2018/12/6 15:25
         * @description: 格式化时间(时:分:秒)
         * @param {Date | number | string}value
         * @param {string}joinSymbol    连接符
         * @param {string}unFilterSymbol    适配失败时返回的字符串
         * @returns {string}
         */
        timeFormat: function(value, joinSymbol, unFilterSymbol) {
            var joinStr = joinSymbol || '-',
                unFilterSymbol = unFilterSymbol || '-';
            if (!value) return unFilterSymbol;
            var time = [],
                data = _dateFormat(value);
            for (var i = 0; i < data.time.length; i++) {
                time.push(preZero(data.time[i]));
            }
            return time.join(joinStr);
        },

        /**
         * @author: ares
         * @date: 2019/1/16 13:44
         * @description: 格式化时间(时:分)
         * @param {Date | number | string}value
         * @param {string}joinSymbol    连接符
         * @param {string}unFilterSymbol    适配失败时返回的字符串
         * @returns {string}
         */
        timeFormatWithOutSecond: function(value, joinSymbol, unFilterSymbol) {
            var joinStr = joinSymbol || '-',
                unFilterSymbol = unFilterSymbol || '-';
            if (!value) return unFilterSymbol;
            var time = [],
                data = _dateFormat(value);
            for (var i = 0; i < data.time.length; i++) {
                time.push(preZero(data.time[i]));
            }
            return time[0] + joinStr + time[1];
        },


        /**
         * @author: ares
         * @date: 2018/12/6 15:26
         * @description: 格式化时间
         * @param {Date | number | string}value
         * @param {string}joinSymbol1    日期连接符
         * @param {string}joinSymbol2    时间连接符
         * @param {boolean}linefeed       日期和时间之间时候换行
         * @param {string}unFilterSymbol    适配失败时返回的字符串
         * @returns {string}
         */
        dateTimeFormat: function(value, joinSymbol1, joinSymbol2, linefeed, unFilterSymbol) {
            var joinStr1 = joinSymbol1 || '-',
                joinStr2 = joinSymbol2 || ':',
                unFilterSymbol = unFilterSymbol || '-';
            if (!value) return unFilterSymbol;

            var date = [],
                time = [],
                br = linefeed ? '<br />' : ' ',
                data = _dateFormat(value);
            for (var i = 0; i < data.date.length; i++) {
                date.push(preZero(data.date[i]));
            }
            for (var i = 0; i < data.time.length; i++) {
                time.push(preZero(data.time[i]));
            }
            return date.join(joinStr1) + br + time.join(joinStr2);
        }
    };

	exports('dateFilters',new DateFilter());
})